package com.afdor.rws.event.autoconfigure.listener;

import com.afdor.rws.core.extension.factory.SpringExtensionFactory;
import com.afdor.rws.event.EventBus;
import com.afdor.rws.event.EventBusLoadedListener;
import com.afdor.rws.event.channel.ChannelProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Spring容器中，事件总线初始化成功监听器，用于在事件总线初始化成功后添加事件源配置
 *
 * @author 悭梵
 * @date Created in 2018-08-27 14:46
 */
public interface EventBusApplicationLoadedListener extends ApplicationListener<ContextRefreshedEvent>, EventBusLoadedListener {

    @Override
    default void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext context = event.getApplicationContext();
        SpringExtensionFactory.setApplicationContext(context);

        EventBus eventBus = context.getBean("defaultEventBus", EventBus.class);
        ChannelProvider channelProvider = context.getBean("defaultChannelProvider", ChannelProvider.class);

        onLoaded(eventBus, channelProvider);
    }
}
